package com.uetoken.cn.activity;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBar;
import com.uetoken.cn.R;
import com.uetoken.cn.base.BaseActivity;
import com.uetoken.cn.bean.MsgBean;
import com.uetoken.cn.common.ApiHelper;
import com.uetoken.cn.network.OkHttpUtils;
import com.uetoken.cn.network.Params;
import com.uetoken.cn.network.WebResponse;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class AddChildPurseActivity extends BaseActivity implements WebResponse {
    private static final String TAG = "AddChildPurseActivity==";
    @BindView(R.id.topbar)
    QMUITopBar mTopbar;
    @BindView(R.id.addChildPurseTv)
    TextView addChildPurseTv;
    @BindView(R.id.addChildPurseEt)
    EditText mAddChildPurseEt;
    @BindView(R.id.addChildPurseLayout)
    RelativeLayout addChildPurseLayout;
    @BindView(R.id.addChildPurseExplainTv)
    TextView addChildPurseExplainTv;
    @BindView(R.id.addChildPurseBtn)
    Button mAddChildPurseBtn;
    @BindView(R.id.delIv)
    ImageView mDelIv;

    /**
     * 钱包创建子钱包请求码
     */
    public static final int NETWORK_REQUEST_CREARE_SUB_PURSE = 10;
    /**
     * 主钱包ID
     */
    private String purseId;
    /**
     * 子钱包名称
     */
    private String childPurseName;

    @Override
    public int getLayoutId() {
        return R.layout.activity_add_child_purse;
    }

    @Override
    public void initView() {
        // 沉浸式状态栏
        QMUIStatusBarHelper.translucent(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            QMUIStatusBarHelper.setStatusBarLightMode(this);
        }
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        Intent intent = getIntent();
        purseId = intent.getStringExtra("purseId");
        mAddChildPurseBtn.setBackgroundResource(R.drawable.gray_circular_bead_shape);
        mAddChildPurseBtn.setClickable(false);
        initTopBar();
        initTextChangedListener();
    }

    @Override
    public void init() {

    }

    private void initTopBar() {
        mTopbar.setTitle(getResources().getString(R.string.str_new_child_purse_title));
        mTopbar.addLeftImageButton(R.drawable.ic_back, R.id.qmui_topbar_item_left_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    public void initTextChangedListener() {
        //转账金额输入框
        mAddChildPurseEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //输入文本之前的状态
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //输入文字中的状态，count是一次性输入字符数
                if (s.length() > 0) {
                    mDelIv.setVisibility(View.VISIBLE);
                    mAddChildPurseBtn.setBackgroundResource(R.drawable.yellow_circular_bead_shape);
                    mAddChildPurseBtn.setClickable(true);
                } else {
                    mDelIv.setVisibility(View.GONE);
                    mAddChildPurseBtn.setBackgroundResource(R.drawable.gray_circular_bead_shape);
                    mAddChildPurseBtn.setClickable(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                //输入文字后的状态
            }
        });
    }

    private void addChildPurse() {
        childPurseName = mAddChildPurseEt.getText() + "";
        if (childPurseName == null || "".equals(childPurseName) || "null".equals(childPurseName)) {
            ToastUtils.showShort(getResources().getString(R.string.str_new_child_purse_toast));
            return;
        }
        showLoading();
        OkHttpUtils.getInstance().getStringWithParam(this, ApiHelper.getCreateSubPurseUrl(), NETWORK_REQUEST_CREARE_SUB_PURSE,
                Params.getCreateSubPurseParams(purseId, childPurseName));
    }

    @Override
    public void onSuccessResponse(Call call, String response, int requestCode) throws IOException {
        LogUtils.dTag(TAG, "onSuccessResponse== response = " + response);
        dissmissDialog();
        switch (requestCode) {
            case NETWORK_REQUEST_CREARE_SUB_PURSE:
                //{"result":1,"message":"创建成功"}
                MsgBean mMsgBean = new Gson().fromJson(response, MsgBean.class);
                ToastUtils.showShort(mMsgBean.getMessage());
                if (mMsgBean.getResult() > 0) {
                    finish();
                }
                break;
        }
    }

    @Override
    public void onFailResponse(Call call, IOException e, int requestCode) {
        dissmissDialog();
        ToastUtils.showShort(R.string.network_anomaly_please_again);
    }

    @OnClick({R.id.delIv, R.id.addChildPurseBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.delIv:
                mAddChildPurseEt.setText("");
                break;
            case R.id.addChildPurseBtn:
                addChildPurse();
                break;
        }
    }
}
